/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 * 
 * Contact luchyx@riaevolution.com
 * See www.riaevolution.com
 *  
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 * */
package com.riaevolution.utils
{
	import flash.display.Bitmap;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Image;
	import mx.core.UIComponent;
	/**
	 * Need documentation
	 *  
	 * @author Luciano Bustos
	 * 
	 */	
	public class IconEmbed extends Image
	{
		public function IconEmbed(source:Class=null) {
			
			super();
			if(source) {
				var img:Bitmap = new source();
			}
			this.icon = source;
		}
		private var _source:Bitmap;	
		
		public function set icon(source:Class):void {
			if(source) {
				if(this.numChildren>0) {
					this.removeChildAt(0);
				}
				_source = new source();
				this.width = _source.width;
				this.height = _source.height;
				this.addChild(_source);
			}
		}
		public function get icon():Class {
			return _source as Class;
		}
		private function updateSize(value:Object):void {
			this.x = holder.width + 4;
		}
		private var holder:UIComponent;
		public function addOnComponent(ui:UIComponent, location:String):void {
			if(ui) {
				holder = ui;
				if(location == 'right') {
					this.x = holder.width + 4;
					BindingUtils.bindSetter(updateSize ,holder ,'width');
				} else {
					this.x = 0;
				}
				this.y = holder.height/2 - this.height/2 + 1;
				holder.addChild(this);
			}
		}
		
	}
}